package com.lcq.subject.domain.handler.subject;

import com.alibaba.fastjson.JSON;
import com.lcq.subject.common.enums.IsDeletedFlagEnum;
import com.lcq.subject.common.enums.SubjectInfoTypeEnum;
import com.lcq.subject.domain.convert.RadioSubjectBOConverter;
import com.lcq.subject.domain.entity.SubjectAnswerBO;
import com.lcq.subject.domain.entity.SubjectInfoBO;
import com.lcq.subject.domain.entity.SubjectOptionBO;
import com.lcq.subject.infra.basic.entity.SubjectRadio;
import com.lcq.subject.infra.basic.service.SubjectRadioService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 单选题目的策略类
 */
@Component
public class RadioTypeHandler implements SubjectTypeHandler {
    
    @Resource
    private SubjectRadioService subjectRadioService;
    
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    /**
     * 单选题目的插入
     * @param subjectInfoBO
     */
    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        List<SubjectRadio> subjectRadioList = new ArrayList<>();
        subjectInfoBO.getOptionList().forEach(option -> {
            SubjectRadio subjectRadio = RadioSubjectBOConverter.INSTANCE.convertAnswerBoToRadio(option);
            subjectRadio.setSubjectId(subjectInfoBO.getId());
            subjectRadio.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            subjectRadioList.add(subjectRadio);
            System.out.println(JSON.toJSONString(subjectRadio));
        });
        subjectRadioService.batchInsert(subjectRadioList);
    }

    @Override
    public SubjectOptionBO query(int subjectId) {
        SubjectRadio subjectRadio = new SubjectRadio();
        subjectRadio.setSubjectId((long) subjectId);
        List<SubjectRadio> subjectRadios = subjectRadioService.queryByCondition(subjectRadio);
        List<SubjectAnswerBO> subjectAnswerBOList = RadioSubjectBOConverter.INSTANCE.convertEntityToBoList(subjectRadios);
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;
    }
}
